package anotador;

import gate.*;
import gate.creole.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.*;
import java.util.*;
import java.util.ArrayList;

/**
 * @author Mauricio
 */

public class GateCorpus{
    
    private Corpus corpus;
    private List<GateDocument> documents;

    private ArrayList<String> outputAnnotations;

    public Corpus getCorpus() {
        return corpus;
    }

    public GateCorpus() {
        try {
            corpus = Factory.newCorpus("GateCorpus");
            documents = new ArrayList<GateDocument>();
        } catch (ResourceInstantiationException ex) {
            ex.printStackTrace();
        }
    }

    public void addDocument(URL url) throws ResourceInstantiationException
    {
        GateDocument doc = new GateDocument(url);
        documents.add(doc);
        corpus.add(doc.getDoc());
    }

    public void setOutputAnnotations(ArrayList<String> outputAnnotations) {
        this.outputAnnotations = outputAnnotations;
    }

    public void crearHTMLs()
    {
        Iterator it = documents.iterator();
        int i = 1;
        while(it.hasNext())
        {
            GateDocument d = (GateDocument)it.next();
            FileOutputStream fos=null;
            try {
                System.out.println(d.getName());
                fos = new FileOutputStream(new File(d.getName()+".html"));
                fos.write(d.toHTML(outputAnnotations).getBytes());
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally
            {
                try {
                    fos.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            i++;
        }
    }
}
